package com.tjetc.dao.impl;

import com.tjetc.dao.NotesDao;
import com.tjetc.domain.Notes;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class NotesDaoImpl implements NotesDao {
    @Override
    public void firstAdd(Notes notes) throws SQLException {
        Db.runner().update(Db.getConnection(),"insert into notes(uid, nid, note) values (?,?,?)",
                notes.getUid(),notes.getNid(),notes.getNote());
    }

    @Override
    public void add(Notes notes) throws SQLException {
        Db.runner().update(Db.getConnection(),"insert into notes(uid, nid, note,mid, num) values (?,?,?,?,?)",
                notes.getUid(),notes.getNid(),notes.getNote(),notes.getMid(),notes.getNum());
    }

    @Override
    public int getCount(int nid) throws SQLException {
        return ((Long)Db.runner0().query("select count(id) from notes where nid = ?",
                new ScalarHandler<>(),nid)).intValue();
    }

    @Override
    public List<Notes> list(PageInfo<Notes> pageInfo,int nid) throws SQLException {
        return Db.runner0().query("select * from notes where nid = ? limit ?,? ",
                new BeanListHandler<>(Notes.class),nid, pageInfo.getStartRecord(),pageInfo.getPageSize());
    }

    @Override
    public List<Notes> listMyNotes(int nid) throws SQLException {
        return Db.runner0().query("select * from notes where nid = ?",new BeanListHandler<>(Notes.class),nid);
    }

    @Override
    public Notes findByNid(int id) throws SQLException {
        return Db.runner0().query("select * from notes where nid=?",new BeanHandler<>(Notes.class),id);
    }

    @Override
    public void delNotes(int uid) throws SQLException {
        Db.runner().update(Db.getConnection(),"delete from notes where uid = ?",uid);
    }

}
